// Package routes 注册路由
package routes

import (
	"github.com/gin-gonic/gin"
	"niu-video/app/controller"
	"niu-video/app/middleware"
	"niu-video/config"
)

func InitRouter() *gin.Engine {

	//初始化路由
	r := gin.New()
	registerGlobalMiddleWare(r)
	registerAPIRoutes(r)
	return r
}

func registerGlobalMiddleWare(router *gin.Engine) {
	router.Use(
		middleware.Logger(),
	)
}

// RegisterAPIRoutes 注册 API 相关路由
func registerAPIRoutes(r *gin.Engine) {

	rg := r.Group(config.Conf.System.UrlPathPrefix)

	vc := new(controller.VideoController)
	uc := new(controller.UserController)
	cc := new(controller.CategoryController)
	lc := new(controller.LikeController)
	coc := new(controller.CollectController)
	rc := new(controller.RelationController)
	commentc := new(controller.CommentController)

	// 评论列表未登录可以访问
	commentGroup := rg.Group("/comment")
	commentGroup.GET("/list", commentc.GetCommentList)

	rg.POST("/register", uc.Register)
	rg.POST("/login", uc.Login)
	rg.POST("/reset", uc.Reset)
	{
		categoryGroup := rg.Group("/category")
		categoryGroup.GET("/list", cc.List)
	}
	rg.Use(middleware.AuthJWT())
	{
		videoGroup := rg.Group("/video")
		videoGroup.POST("/token", vc.Token)
		videoGroup.GET("/proxy", vc.GetImageByProxy)
		videoGroup.GET("/list", vc.List)
		videoGroup.POST("/publish", vc.Publish)
		videoGroup.POST("/userWork", vc.UserWork)
		videoGroup.POST("/userLike", vc.UserLike)
		videoGroup.POST("/userCollect", vc.UserCollect)
		videoGroup.GET("/delete", vc.Delete)
		videoGroup.POST("/category", vc.Category)
		videoGroup.POST("/search", vc.Search)
	}
	{
		likeGroup := rg.Group("/like")
		likeGroup.POST("/action", lc.Like)
	}

	{
		cocGroup := rg.Group("/collect")
		cocGroup.POST("/action", coc.Collect)
	}
	{
		rcGroup := rg.Group("/relation")
		rcGroup.POST("/action", rc.Relation)
		rcGroup.POST("/follow", rc.Follow)
	}
	{
		usersGroup := rg.Group("/user")
		usersGroup.GET("/info", uc.Info)
		usersGroup.GET("/get", uc.Get)
		usersGroup.POST("/update", uc.Update)
		usersGroup.POST("/action", uc.Action)
	}
	{
		commentGroup := rg.Group("/comment")
		commentGroup.POST("/create", commentc.Create)
		commentGroup.POST("/delete", commentc.Delete)
	}
}
